<?php

namespace App\Admin\Forms;

use App\Models\User;
use Dcat\Admin\Widgets\Form;
use Symfony\Component\HttpFoundation\Response;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;

class JoinTeam extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return Response
     */
    public function handle(array $input)
    {

        // 获取外部传递参数
        $user_id = $input['user_id'] ?? null;
        $team_id = $input['team_id'] ?? null;
        //更新
        $res = User::whereIn('id',$user_id)->update(['team_id'=>$team_id,'update_time' => time()]);
        if ($res){
            return $this->success('加入成功', '/User');
        }else{
            return $this->success('加入失败', '/User');
        }
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->select('team_id','球队')->options('team/teamData');
        $this->select('user_id','球员')->options('team/notPlayer');
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'name'  => 'John Doe',
            'email' => 'John.Doe@gmail.com',
        ];
    }
}
